//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Discussions.Components;
using CommunityServer.Discussions.Controls;

namespace CommunityServer.ControlPanel.Moderation
{
	/// <summary>
	/// Summary description for ModerateForum.
	/// </summary>
	public class ModerateForum : BaseModerationPage
	{
		CSContext csContext;
		protected Control NoPostsToModerate;
		protected Repeater PostRepeater;
		int postCount = 0;

		protected override void OnInit(EventArgs e)
		{
			csContext = CSContext.Current;

			Permissions.AccessCheck(CommunityServer.Discussions.Components.Forums.GetForum(csContext.SectionID), Permission.Moderate, csContext.User);

			this.Load += new EventHandler(this.Page_Load);
			PostRepeater.ItemDataBound += new RepeaterItemEventHandler(PostRepeater_ItemDataBound);

			AjaxManager.Register(this,"Moderation", true, AjaxDebug.None);

			base.OnInit(e);
		}

		private void BuildCallbackScript()
		{
			StringBuilder sb = new StringBuilder();

			sb.Append("\n<script language='javascript' type=\"text/javascript\">\n");
			sb.AppendFormat("\nvar postCount = {0};\n", postCount);
			sb.Append("\nfunction DoApprovePost(containerID, postID)\n{\n\tModeration.ApprovePostAjax(containerID, postID, ApprovePostCallback);\n}\n");
			sb.Append("\nfunction DoApproveViewPost(containerID, postID)\n{\n\tModeration.ApprovePostAjax(containerID, postID, ApproveViewPostCallback);\n}\n");
			sb.Append("\nfunction DoToggleModeration(containerID, userID, controlID)\n{\n\tModeration.ToggleModerationAjax(containerID, userID, controlID, ToggleModerationCallback);\n}\n");
			sb.Append("\nfunction DoQuickAction(url)\n{\n\tTelligent_Modal.Open(url, 600, 500, QuickActionCallback);\n}\n");
			sb.Append("\nfunction DoQuickHistory(url)\n{\n\tTelligent_Modal.Open(url, 600, 300, null);\n}\n");
			sb.Append("\nfunction ApprovePostCallback(response)\n{\n\tHidePost(response.value);\n}\n");
			sb.Append("\nfunction ApproveViewPostCallback(response)\n{\n\tViewPost(response.value);\n}\n");
			sb.Append("\nfunction ToggleModerationCallback(response)\n{\n\tSetText(response.value[0], response.value[1]);\n}\n");
			sb.Append("\nfunction QuickActionCallback(postID)\n{\n\tif (postID != -1)\n\t\tHidePost(postID);\n}\n");
			sb.Append("\nfunction HidePost(postID)\n{\n\tpostCount--;\n\tif (postCount <= 0)\n\t\twindow.location.href='" + Globals.GetSiteUrls().ModerationHome + "';\n\n\tvar control = document.getElementById('Post_' + postID);\n\tcontrol.style.display = 'none';\n}\n");
			sb.Append("\nfunction ViewPost(postID)\n{\n\twindow.location.href = '" + Globals.GetSiteUrls().ModeratePostRedirect + "' + postID;\n}\n");
			sb.Append("\nfunction SetText(controlID, text)\n{\n\tvar control = document.getElementById(controlID);\n\tcontrol.innerHTML = text;\n}\n");
			sb.Append("\n</script>");

			Page.RegisterClientScriptBlock("DoWrappersAndAJAXCallbacks", sb.ToString());
		}

		private void Page_Load(object sender, EventArgs e)
		{
			ArrayList posts = Moderate.GetPosts(CSContext.Current.SectionID, 0, 25, 0, 0).Posts;
			postCount = posts.Count;

			if (postCount <= 0)
				NoPostsToModerate.Visible = true;

			PostRepeater.DataSource = posts;
			PostRepeater.DataBind();

			BuildCallbackScript();
		}

		private void PostRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Post post = (Post)e.Item.DataItem;

			HyperLink Approve = (HyperLink)e.Item.FindControl("Approve");
			if (Approve != null)
			{
				Approve.Text = ResourceManager.GetString("ModerationMenu_Approve");
				Approve.NavigateUrl = string.Format("javascript:DoApprovePost('{0}', {1});", Approve.ClientID, post.PostID.ToString());
			}

			HyperLink ApproveView = (HyperLink)e.Item.FindControl("ApproveView");
			if (ApproveView != null)
			{
				ApproveView.Text = ResourceManager.GetString("ModerationMenu_ApproveView");
				ApproveView.NavigateUrl = string.Format("javascript:DoApproveViewPost('{0}', {1});", ApproveView.ClientID, post.PostID.ToString());
			}

			HyperLink Delete = (HyperLink)e.Item.FindControl("Delete");
			if (Delete != null)
			{
				if (post.PostLevel == 1) 
					Delete.Text = ResourceManager.GetString("ModerationMenu_DeleteThread");
				else
					Delete.Text = ResourceManager.GetString("ModerationMenu_DeletePost");

				Delete.NavigateUrl = Globals.GetSiteUrls().ModeratePostDelete(post.PostID, Globals.UrlEncode(HttpContext.Current.Request.Url.PathAndQuery));
			}

			HyperLink JoinSplit = (HyperLink)e.Item.FindControl("JoinSplit");
			if (JoinSplit != null)
			{
				if (post.PostLevel > 1)
				{
					JoinSplit.Text = ResourceManager.GetString("ModerateThread_Split");
					JoinSplit.NavigateUrl = string.Format("javascript:DoQuickAction('{0}')", Globals.GetSiteUrls().ModerateQuickThreadSplit(post.PostID));
				} 
				else 
				{
					JoinSplit.Text = ResourceManager.GetString("ModerateThread_Join");
					JoinSplit.NavigateUrl = string.Format("javascript:DoQuickAction('{0}')", Globals.GetSiteUrls().ModerateQuickThreadJoin(post.PostID));
				}
			}

			HyperLink Move = (HyperLink)e.Item.FindControl("Move");
			if (Move != null)
			{
				Move.Text = ResourceManager.GetString("ModeratePost_Move");

				if (post.PostLevel == 1)
					Move.NavigateUrl = Globals.GetSiteUrls().ModeratePostMove(post.PostID, Globals.UrlEncode(HttpContext.Current.Request.Url.PathAndQuery));
				else
					Move.ForeColor = System.Drawing.Color.Gray;
			}

			HyperLink EditUser = (HyperLink)e.Item.FindControl("EditUser");
			if (EditUser != null)
			{
				if (csContext.User.IsAdministrator)
				{
					EditUser.Visible = true;
					EditUser.Text = ResourceManager.GetString("ModeratePost_EditUser");
					EditUser.NavigateUrl = Globals.GetSiteUrls().ControlPanelUserEdit(post.User.UserID);
				}
				else
				{
					EditUser.Visible = false;
				}
			}

			HyperLink ToggleUserModeration = (HyperLink)e.Item.FindControl("ToggleUserModeration");
			if (ToggleUserModeration != null)
			{
				switch (post.User.ModerationLevel)
				{
					case ModerationLevel.Unmoderated:
						ToggleUserModeration.Text = ResourceManager.GetString("ModerateThread_Moderate_User");
						break;
					case ModerationLevel.Moderated:
						ToggleUserModeration.Text = ResourceManager.GetString("ModerateThread_UnModerate_User");
						break;
					default:
						ToggleUserModeration.Enabled = false;
						break;
				}

				ToggleUserModeration.NavigateUrl = string.Format("javascript:DoToggleModeration('{0}', {1}, '{2}');", e.Item.ClientID, post.User.UserID.ToString(), ToggleUserModeration.ClientID);
			}

			HyperLink ModerationHistory = (HyperLink)e.Item.FindControl("ModerationHistory");
			if ((ModerationHistory != null) && (csContext.SiteSettings.EnableUserModerationCounters))
			{
				ModerationHistory.Visible = true;
				ModerationHistory.Text = ResourceManager.GetString("ModeratePost_ModerationHistory");
				ModerationHistory.NavigateUrl = string.Format("javascript:DoQuickHistory('{0}')", Globals.GetSiteUrls().ModerationQuickHistory(post.PostID));
			}
			else
			{
				if (ModerationHistory != null)
					ModerationHistory.Visible = false;
			}

			PostFlatPreview PostPreview = (PostFlatPreview)e.Item.FindControl("PostPreview");
			if (PostPreview != null)
			{
				PostPreview.DataSource = new object[] {post};
				PostPreview.DataBind();
			}
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=false)]
		public string ApprovePostAjax(int postID)
		{
			ForumPost post = Posts.GetPost(postID, csContext.User.UserID);
			Moderate.ApprovePost(post, csContext.User.UserID);

			return postID.ToString();
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=false)]
		public string[] ToggleModerationAjax(int userID, string controlID)
		{
			User user = Users.GetUser(userID, false, false);
			Moderate.ToggleUserSettings(ModerateUserSetting.ToggleModerate, user, csContext.User.UserID);

			string text = (user.ModerationLevel == ModerationLevel.Moderated) ? ResourceManager.GetString("ModerateThread_Moderate_User") : ResourceManager.GetString("ModerateThread_UnModerate_User");
			return new string[] {controlID, text};
		}
	}
}
